id/email
password
forgot password | create account
about | help | prefs
ReadingBatcode reading practice

 

 

Collection TransformationsMap2

val isInRange1: (Int) -> Boolean = { i: Int -> i in 3..6 } val isInRange2: (Int) -> Boolean = { it < 2 || it > 5 } fun mapIt2Long(i: Int, func: (Int) -> Boolean): List<Boolean> { val list = mutableListOf<Boolean>() for (v in (0..i)) { // (0..i) is inclusive val w = func.invoke(v) list.add(w) } return list } fun mapIt2Short(i: Int, func: (Int) -> Boolean): List<Boolean> = (0..i).map(func) fun main() { println(mapIt2Long(4, isInRange1)) println(mapIt2Long(4, isInRange2)) println(mapIt2Short(2, isInRange1)) println(mapIt2Short(2, isInRange2)) println(mapIt2Short(4) { i: Int -> i > 4 }) println(mapIt2Short(4) { i: Int -> i < 5 }) println(mapIt2Short(4) { it == 5 }) println(mapIt2Short(4) { it == 4 }) }

Click on to run the code

⬅ Back